// const baseUrl = 'https://www.qwcfcenter.com/xcxapi/'
const baseUrl = 'https://shangda.doing.net.cn/xcxapi/'

const request = (url, params={}) => {
  const app = getApp();
  return new Promise((resolve, reject) => {
    let { token } = app.globalData.userInfo || {}

    if (token == '' || token == undefined || token == null || token == false) {
      token = ''
    }
    let {
      data,
      method = 'POST', // 默认post
      header = {},
      show = false //是否显示成功弹窗
    } = params;
    wx.request({
      url: baseUrl + url,
      data: { ...data,token },
      method,
      header:{
        'Content-Type': 'application/x-www-form-urlencoded',
        ...header
      },
      success:({ data, errMsg, statusCode })=>{
       
        // return
        if(data.errcode==0 || statusCode!=200 || data.errcode!=1){//这里条件漏一部分
            if (data.errcode == 0) {
              console.log('成功了');
              wx.showToast({
                title:data.message,
                icon:'none'
              })
            }else{
              wx.showToast({
                title: '加载中...' || data.message || errMsg, icon:'none'
              })
            }

          if(data.errcode==401 || statusCode==401 || data.errcode==-100){ //登录失效
            // wx.clearStorage();
            wx.removeStorage({
              key: '_userInfo',
            });
            getApp().globalData.userInfo = null
            wx.redirectTo({
              url: '/pages/login/login'
            })
          }
          reject();
        }else{
          console.log('成功了');
          if (show) {
            wx.showToast({
              title:  data.message || '操作成功', icon:'success'
            })
          }

          resolve(data)
        }
      },
      fail:error=>{
        console.log('失败了');
        wx.showToast({
          title: '加载中on...' || data.message || errMsg, icon:'none'
        })
        // console.log('这')
        reject(data) 
      
      }
    })
  })
}
module.exports = {
  baseUrl,
  request
}